package com.szqhyxj.youxiangju.biz.plan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.plan.bridge.PlanDetailBridge;
import com.szqhyxj.youxiangju.biz.web.WebExit;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PlanCreateSuccessActivity extends BaseActivity implements NavigationBar.NavigationBarListener {

    private static final String PLAN_DETAIL = "PLAN_DETAIL";

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.back_to_home)
    Button backToHome;
    @BindView(R.id.view_detail)
    Button viewDetail;
    @BindView(R.id.amount)
    TextView amount;

    private PlanDetail planDetail;

    public static void start(Activity activity, PlanDetail planDetail) {
        Intent intent = new Intent();
        intent.putExtra(PLAN_DETAIL, planDetail);
        intent.setClass(activity, PlanCreateSuccessActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_success);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        extractIntent(intent);
        navigationBar.setNavigationBarListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MerchantContainerActivity.start(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        extractIntent(intent);
    }

    private void extractIntent(Intent intent) {
        this.planDetail = intent.getParcelableExtra(PLAN_DETAIL);
        amount.setText(String.format(Locale.getDefault(), "%.2f", planDetail.getWorkingAmount()) + " 元");
    }

    private void navToDetail() {
        finish();
    }

    @OnClick({R.id.back_to_home, R.id.view_detail})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_to_home:
                RepaymentActivity.start(this);
                break;
            case R.id.view_detail:
                // PlanParticularsActivity.start(this, planDetail, PlanModel.PRESENT);
                // finish();
                PlanDetailBridge.showPlanDetail(this, planDetail.getPlanId(), WebExit.PLAN_LIST.getCode());
                break;
        }
    }

    @Override
    public void navigationLeft() {
        RepaymentActivity.start(this);
        finish();
    }

    @Override
    public void navigationRight() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            RepaymentActivity.start(this);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}